package com.study.samsung.Control.htControl;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import com.study.samsung.Entity.Classification;
import com.study.samsung.Entity.Commodity;
import com.study.samsung.Entity.Commodityimg;
import com.study.samsung.Entity.Succession;
import com.study.samsung.Service.ClassificationService;
import com.study.samsung.Service.CommodityService;
import com.study.samsung.Service.CommodityimgService;
import com.study.samsung.Service.SuccessionService;
import com.study.samsung.Utils.BaseServlet;
import com.study.samsung.Utils.PartUtil;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import javax.swing.*;
import java.awt.*;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

@WebServlet("/goodsedit")
@MultipartConfig
public class GoodsEditServlet extends BaseServlet {

    CommodityService cs = new CommodityService();
    ClassificationService cls = new ClassificationService();
    CommodityimgService cis = new CommodityimgService();
    @Override
    public String excute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        Object obj = session.getAttribute("admin");
        if (obj==null){
            return "f:htjsp/login";
        }else {
            String Sid = request.getParameter("sid");
            int i = Integer.parseInt(Sid);

            List<Classification> classification = cls.selectAll();
            session.setAttribute("classification",classification);
            List<Commodity> commodities = cs.htselectByid(i);
            System.out.println("根据id："+i+"查询到的单条数据"+commodities);
            session.setAttribute("goodslist",commodities);
            List<Commodityimg> commodityimgs = cis.selectAll();
            System.out.println("查询到的所有图片id："+commodityimgs);
            session.setAttribute("commodityimgs",commodityimgs);
            return "f:htjsp/goodsedit";
        }

    }

    public String goodsEdit(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        PartUtil partUtil = new PartUtil();
        String oldName;
        String stoPath;
        String outPath;
        String newName;
        response.setContentType("application/json;charset=utf-8");
        String Sid = request.getParameter("level-id");
        Integer sid = Integer.parseInt(Sid);
        String sname = request.getParameter("level-name");
        String Sprcie = request.getParameter("level-price");
        BigDecimal sprcie = new BigDecimal(Sprcie);
        String Lid = request.getParameter("level-leibie");
        Integer lid = Integer.parseInt(Lid);

        Part part = request.getPart("myfile");
        String myfile=null;
        String realPath = request.getServletContext().getRealPath("/");
        oldName = part.getSubmittedFileName();//获取文件的原始名字
        if (part.getSize()>0){
            //D:\Java03\YF03405\02Java\code\Java\Git\meizu\out\artifacts\meizu_Web_exploded\
            stoPath=realPath.substring(0,realPath.indexOf("out"))+"web/img/"+sname;
            outPath=realPath+"img/"+sname;
            newName =getSuffix(oldName);
            //上传头像文件
            FileUtil.writeFromStream(part.getInputStream(),stoPath+newName);
            myfile = "../img/"+sname+newName;
        }else {
            List<Commodity> commodities = cs.htselectByid(sid);
            myfile = commodities.get(0).getUrl();
        }

        Commodity commodity=new Commodity (sid,sname,myfile,sprcie,true,lid);
        Integer update = cs.Update(commodity);

        String hiddeninput1 = request.getParameter("hiddeninput1");
        String hiddeninput2 = request.getParameter("hiddeninput2");
        String hiddeninput3 = request.getParameter("hiddeninput3");
        String hiddeninput4 = request.getParameter("hiddeninput4");
        String hiddeninput5 = request.getParameter("hiddeninput5");
        String hiddeninput6 = request.getParameter("hiddeninput6");
        String hiddeninput7 = request.getParameter("hiddeninput7");

        List<String> imgsrc2 = new ArrayList<>();
        imgsrc2.add(hiddeninput1);
        imgsrc2.add(hiddeninput2);
        imgsrc2.add(hiddeninput3);
        imgsrc2.add(hiddeninput4);
        imgsrc2.add(hiddeninput5);
        imgsrc2.add(hiddeninput6);
        imgsrc2.add(hiddeninput7);

        Part myinput1 = request.getPart("myinput1");
        Part myinput2 = request.getPart("myinput2");
        Part myinput3 = request.getPart("myinput3");
        Part myinput4 = request.getPart("myinput4");
        Part myinput5 = request.getPart("myinput5");
        Part myinput6 = request.getPart("myinput6");
        Part myinput7 = request.getPart("myinput7");

        List<Part> part1 = new ArrayList<>();
        part1.add(myinput1);
        part1.add(myinput2);
        part1.add(myinput3);
        part1.add(myinput4);
        part1.add(myinput5);
        part1.add(myinput6);
        part1.add(myinput7);

        List<String> imgsrc = new ArrayList<>();

        for (int i = 0; i < part1.size(); i++) {
                imgsrc.add(partUtil.uploadImage(part1.get(i), realPath));
        }
        System.out.println(imgsrc);
        List<Commodityimg> commodityimgs = cis.selectByid(sid);
        for (int i = 0; i < commodityimgs.size(); i++) {
            if (part1.get(i).getSize()!=0){
                cis.updateurl(commodityimgs.get(i).getTid(),imgsrc.get(i));
            }else{
                cis.updateurl(commodityimgs.get(i).getTid(),imgsrc2.get(i));
            }

        }


        return null;
    }


    //获取文件后缀名
    public String getSuffix(String oldName){
        //.png/.jpg……
        int i = oldName.lastIndexOf(".");
        return oldName.substring(i);
    }
    //生成唯一的文件名
    public String uniqeName(){
        //获取当前系统时间
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String s=sdf.format(date);
        //生成四位数的随机数  1-9999之间，不够四位的，补齐四位 0001  0025
        int rnd = (int)(Math.random()*9999)+1;
        //rnd=4
        String n = rnd+"";
        for(int i=0;i<(4-n.length());i++){
            n="0"+n;
        }
        return s+n;
    }


}
